//
//  CollectionDetailViewController.m
//  Food
//
//  Created by 15 on 15/7/22.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "CollectionDetailViewController.h"
#import "BaseModel.h"
#import "BaseHeader.h"
#import "HeightCount.h"
#import "AFNet.h"
#import "UIImageView+WebCache.h"
@interface CollectionDetailViewController ()

@end

@implementation CollectionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
        [self getDataFood];
}

- (void)createView
{
    self.scrollViewMain = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49)];
    self.scrollViewMain.contentSize = CGSizeMake(SCREEN_WIDTH, 5000);
    [self.view addSubview:self.scrollViewMain];
    self.imgFood = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 230)] autorelease];
    [self.scrollViewMain addSubview:self.imgFood];
    [self.imgFood setImageWithURL:[NSURL URLWithString:self.model.img] placeholderImage:[UIImage imageNamed:@"HomePage1"]];
    self.lblTitle = [[[UILabel alloc] initWithFrame:CGRectMake(60, self.imgFood.frame.size.height + 5, 200, 22)] autorelease];
    self.lblTitle.text = [self.model name];
    self.lblTitle.font = [UIFont boldSystemFontOfSize:19];
    self.lblTitle.textAlignment = NSTextAlignmentCenter;
    [self.scrollViewMain addSubview:self.lblTitle];
    [self.scrollViewMain release];
    self.lblMaterial = [[UILabel alloc] initWithFrame:CGRectMake(10, self.lblTitle.frame.origin.y + 50, 60, 20)];
    self.lblMaterial.text = @"用料";
    self.lblMaterial.font = [UIFont boldSystemFontOfSize:15];
    [self.scrollViewMain addSubview:self.lblMaterial];
    [self.lblMaterial release];
    // arr为材料数据数组
    NSArray *arr = self.model.burden;
    // i为材料个数 MeterialHeight为最后一个材料的高度 + 20
    NSInteger i = 0;
    // 计算最后一个材料的高度
    NSInteger MeterialHeight = 0;
    // 上一个材料的高度
    NSInteger hight = 0;
    // 上一个材料的高度
    NSInteger hightMeterial = 0;
    for (NSDictionary *d in arr) {
        i++;
        UILabel *lblMaterialName = [[UILabel alloc] initWithFrame:CGRectMake(50, self.lblMaterial.frame.origin.y + hight + 5, 130, 20)];
        lblMaterialName.text = d[@"name"];
        lblMaterialName.font = [UIFont systemFontOfSize:14];
        [self.scrollViewMain addSubview:lblMaterialName];
        [lblMaterialName release];
        UILabel *lblMaterialContent = [[[UILabel alloc] initWithFrame:CGRectMake(180, lblMaterialName.frame.origin.y, 140, 20)] autorelease];
        lblMaterialContent.text = d[@"content"];
        if ([d[@"name"] isEqualToString:@"盐半小勺"]) {
            lblMaterialName.text = @"盐";
        }

        if ([d[@"content"] isEqualToString:@""]) {
            lblMaterialContent.text = @"适量";
        }
        if (d[@"content"] == nil) {
            lblMaterialContent.text = @"适量";
        }
        lblMaterialContent.numberOfLines = 0;
        [lblMaterialContent sizeToFit];
        hight = lblMaterialName.frame.origin.y + lblMaterialContent.frame.size.height - self.lblMaterial.frame.origin.y;
        lblMaterialContent.font = [UIFont systemFontOfSize:14];
        [self.scrollViewMain addSubview:lblMaterialContent];
        MeterialHeight = lblMaterialName.frame.origin.y + lblMaterialContent.frame.size.height;

    }
    UILabel *lblMake = [[UILabel alloc] initWithFrame:CGRectMake(10, MeterialHeight + 20, 50, 20)];
    lblMake.text = @"做法";
    lblMake.font = [UIFont boldSystemFontOfSize:15];
    [self.scrollViewMain addSubview:lblMake];
    [lblMake release];
    // arr1为做法的所有数据数组
    // j 计算步骤的变量
    // highImg为步骤图片底部的坐标
    // highNum为步骤前标的坐标
    // highSpacing 为前一步骤距离步骤开始的坐标
    NSArray *arr1 = self.model.makes;
    NSInteger j = 0;
    NSInteger highImg = 0;
    NSInteger highNum = 0;
    NSInteger highSpacing = 0;
    for (NSDictionary *d in arr1) {
        j++;
        UILabel *lblCount = [[UILabel alloc] initWithFrame:CGRectMake(10, lblMake.frame.origin.y + (j * 20) + highSpacing, 20, 20)];
        lblCount.text = d[@"num"];
        lblCount.font = [UIFont boldSystemFontOfSize:15];
        [self.scrollViewMain addSubview:lblCount];
        [lblCount release];
        UILabel *lblMake = [[[UILabel alloc] initWithFrame:CGRectMake(50, lblCount.frame.origin.y, 240, [HeightCount heightWithText:d[@"info"] font:15 wide:240])]
        autorelease];
        NSString *str1 = [d[@"info"] stringByReplacingOccurrencesOfString:@"<br/>" withString:@"   "];
        lblMake.text = str1;
        highNum = lblCount.frame.origin.y;
        lblMake.numberOfLines = 0;
        lblMake.font = [UIFont systemFontOfSize:15];
        [self.scrollViewMain addSubview:lblMake];
        UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(30, lblMake.frame.origin.y + lblMake.frame.size.height + 3, 250, 160)] autorelease];
        img.contentMode = UIViewContentModeScaleAspectFit;
        [img setImageWithURL:[NSURL URLWithString:d[@"img"]]];
        [self.scrollViewMain addSubview:img];
        highImg = img.frame.origin.y + img.frame.size.height;
        highSpacing += highImg - highNum;
    }

    if ([self.model.healthStr isEqualToString:@""]) {
        self.scrollViewMain.contentSize = CGSizeMake(SCREEN_WIDTH, highImg +80);
    } else {

        UILabel *lblHealth = [[UILabel alloc] initWithFrame:CGRectMake(10, highImg + 20, 100, 20)];
        lblHealth.text = @"养生必读";
        [self.scrollViewMain addSubview:lblHealth];
        [lblHealth release];
        UILabel *lblHealthStr = [[[UILabel alloc] initWithFrame:CGRectMake(10, lblHealth.frame.origin.y + 30, 300, 50)] autorelease];
        NSString *str = [self.model.healthStr stringByReplacingOccurrencesOfString:@"；" withString:@""];
        lblHealthStr.text = str;
        lblHealthStr.numberOfLines = 0;
        [lblHealthStr sizeToFit];
        [self.scrollViewMain addSubview:lblHealthStr];
        self.scrollViewMain.contentSize = CGSizeMake(SCREEN_WIDTH, lblHealthStr.frame.origin.y + lblHealthStr.frame.size.height + 20 + 60);
    }
}

- (void)getDataFood
{
    NSString *strHttp = [NSString stringWithFormat:@"%@%@", ThreeMealDetail, self.Code];
    [AFNet connectNetGetDataWithURLString:strHttp parameters:nil finishblock:^(id result) {
        NSDictionary *dicData = result[@"data"];
        self.model = [[DrawerModel alloc] initWithDic:dicData];
        [self createView];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
